package com.dommy.tab.util;

import android.util.Log;

import org.json.JSONArray;
import org.json.JSONObject;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * HttpUtil
 * Created by 光头 on 2020/7/1.
 */

public class   MyHttpUtil {
    public static Object obj = null;
    public static <T> Object getListToUrl(String url,Class<T> clazz){
        Request request = new Request
                .Builder()
                .url(url).build();
        ArrayList<T> list = new ArrayList<>();
        try {
            String result = new OkHttpClient().newCall(request).execute().body().string();
            JSONArray jsonArray;
            if(isHaveString(result,"data")){
                result = new JSONObject(result).getString("data");
                if(isHaveString(result,"monthList")){
                    result = new JSONObject(result).getString("monthList");
                }
                jsonArray = new JSONArray(result);
            }else{
                jsonArray = new JSONArray(result);
            }
//            getListTOJSON(jsonArray,clazz);
            Field[] fields = clazz.getFields();
            for (int i =0;i<jsonArray.length();i++){
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                T t =  clazz.newInstance();
                for (Field field : fields) {
                    field.setAccessible(true);
                    if(!jsonObject.isNull(field.getName())){
                        if (field.getType().equals(String.class)){
                            field.set(t,jsonObject.get(field.getName()));
                        }else if(field.getType().equals(Integer.class)){
                            field.set(t,Integer.parseInt(jsonObject.get(field.getName()).toString()));
                        }else if (field.getType().equals(Double.class)){
                            field.set(t,Double.parseDouble(jsonObject.get(field.getName()).toString()));
                        }else if(field.getType().equals(Date.class)){
                            field.set(t,Date.parse(jsonObject.get(field.getName()).toString()));
                        }
                    }
                }
                list.add(t);
            }
            obj = list;
        } catch (Exception e) {
            System.out.println("MyHttpUtil-getList-parseJSON出错啦.................");
            e.printStackTrace();
        }
        return obj;
    }

    public static boolean isHaveString(String result,String stc){
        boolean flag =false;
        try{
            if(new JSONObject(result).get(stc)!= null)
                flag = true;
        }catch (Exception e){
            flag = false;
        }
        return flag;
    }

    public static <T> Object getListTOJSON(JSONArray jsonArray,Class<T> clazz) throws Exception{
        ArrayList<T> listTemp = new ArrayList<>();
        Field[] fields = clazz.getFields();
        for (int i =0;i<jsonArray.length();i++){
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            T t =  clazz.newInstance();
            for (Field field : fields) {
                field.setAccessible(true);
                if(!jsonObject.isNull(field.getName())){
                    if (field.getType().equals(String.class)){
                        field.set(t,jsonObject.get(field.getName()));
                    }else if(field.getType().equals(Integer.class)){
                        field.set(t,Integer.parseInt(jsonObject.get(field.getName()).toString()));
                    }else if (field.getType().equals(Double.class)){
                        field.set(t,Double.parseDouble(jsonObject.get(field.getName()).toString()));
                    }else if(field.getType().equals(Date.class)){
                        field.set(t,Date.parse(jsonObject.get(field.getName()).toString()));
                    }
                }
            }
            listTemp.add(t);
        }
        return listTemp;
    }
}
